//app.js
import Touches from './utils/Touches.js'

const douban = require('./utils/douban.js')
var Bmob = require("utils/bmob.js");
var common = require("utils/common.js");
const __utils = require('utils/util')
Bmob.initialize("bf5886964e0b314a7a8b6ca9ce5ff2c2", "9bbf9439423eb1db743467e54585f8c1");
App({
  version: 'v5.2.7', //版本号
  onLaunch: function () {
    var that = this;
    //调用系统API获取设备的信息
    wx.getSystemInfo({
      success: function (res) {
        var kScreenW = res.windowWidth / 375
        var kScreenH = res.windowHeight / 603
        wx.setStorageSync('kScreenW', kScreenW)
        wx.setStorageSync('kScreenH', kScreenH)
        that.globalData.windowWidth = res.windowWidth;
        that.globalData.windowHeight = res.windowHeight;
      }
    })

  },
  onShow: function () {
  },
  onHide: function onHide() {
  },
  onError: function (msg) {
  },
  signUp: function (user) {
    var that = this;
    user.signUp(null, {
      success: function (result) {
        console.log('注册成功');
        user.set('signUpSuccess', true);
        try {//将返回的3rd_session存储到缓存中
          wx.setStorageSync('user_openid', user.get('userData').openid)
          wx.setStorageSync('user_id', user.id)
          wx.setStorageSync('my_nick', user.get("nickname"))
          wx.setStorageSync('my_username', user.get("username"))
          wx.setStorageSync('my_sex', user.get("sex"))
          wx.setStorageSync('my_avatar', user.get("userPic"))
        } catch (e) {
        }
      },
      error: function (userData, error) {
        console.log("openid=" + userData);
        console.log(error)
      }
    });
  },
  formate_data: function (date) {
    let month_add = date.getMonth() + 1;
    var formate_result = date.getFullYear() + '年'
      + month_add + '月'
      + date.getDate() + '日'
      + ' '
      + date.getHours() + '点'
      + date.getMinutes() + '分';
    return formate_result;
  },
  globalData: {
    userInfo: null,
    needRefresh: true,
    windowWidth: 0,
    windowHeight: 0,
    doubanBase: "https://douban.uieee.com",
    inTheaters: "/v2/movie/in_theaters",
    comingSoon: "/v2/movie/coming_soon",
    top250: "/v2/movie/top250",
    weekly: "/v2/movie/weekly",
    usBox: "/v2/movie/us_box",
    newMovies: "/v2/movie/new_movies",
    subject: "/v2/movie/subject/",
    celebrity: "/v2/movie/celebrity/",
    search: "/v2/movie/search?q=",
    review: "/v2/movie/review/",
    doubanVideoUrl:'',
    doubanFMAutoPlay: false
  },
  Touches: new Touches(),
  util: __utils,
  douban: douban,
  showErrorModal: function (content, title) {
    wx.showModal({
      title: title || '加载失败',
      content: content || '未知错误',
      showCancel: false
    });
  },
  showLoadToast: function (title, duration) {
    wx.showToast({
      title: title || '加载中',
      icon: 'loading',
      duration: duration || 10000
    });
  }
})